package com.zonski.jbots.desktop.application;

import com.zonski.jbots.desktop.client.DesktopDevice;
import com.zonski.jbots.engine.InputStreamFactory;
import com.zonski.jbots.engine.Device;

import java.io.InputStream;
import java.io.BufferedInputStream;
import java.util.Hashtable;
import java.util.Iterator;
import javax.sound.sampled.*;

/**
 * Created by IntelliJ IDEA.
 * User: Chris Glover
 * Date: Oct 9, 2003
 * Time: 5:40:41 PM
 * To change this template use Options | File Templates.
 */
public class ApplicationDevice extends DesktopDevice
{
    private InputStreamFactory inputStreamFactory;
    private Mixer mixer;
    private Hashtable clips;

    public ApplicationDevice(InputStreamFactory inputStreamFactory)
    {
        this.inputStreamFactory = inputStreamFactory;
        Mixer.Info[] mixers = AudioSystem.getMixerInfo();
        this.mixer = AudioSystem.getMixer(mixers[0]);
        this.clips = new Hashtable(2);
    }

    public void play(String name, int times)
    {
        AudioInputStream ins = null;
        try
        {
            int actual = times;
            Clip clip;
            switch(times)
            {
                case Device.STOP:
                    clip = (Clip)this.clips.get(name);
                    if(clip != null)
                    {
                        clip.stop();
                    }
                    break;
                case Device.LOOP:
                    actual = Clip.LOOP_CONTINUOUSLY;
                default:
                    clip = (Clip)this.clips.get(name);
                    if(clip == null)
                    {
                        ins = AudioSystem.getAudioInputStream(
                                new BufferedInputStream(this.inputStreamFactory.getInputStream(name))
                        );
                        clip = (Clip)this.mixer.getLine(new Line.Info(Clip.class));
                        clip.open(ins);
                        this.clips.put(name, clip);
                    }
                    if(!clip.isRunning())
                    {
                        clip.setFramePosition(0);
                        if(actual == 1)
                        {
                            clip.start();
                        }else{
                            clip.loop(actual);
                        }
                    }
                    break;

            }
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            if(ins != null)
            {
                try
                {
                    ins.close();
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        }
    }

    public void stopAll()
    {
        super.stopAll();
        Iterator iterator = this.clips.values().iterator();
        while(iterator.hasNext())
        {
            Clip clip = (Clip)iterator.next();
            clip.stop();
        }
    }
}
